
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

/**
 *
 * @author styfle
 */
public class Main extends JPanel implements ActionListener {

    private static final long SAFE_TIME = 750;
    private boolean cursorHidden = false;
    public static JFrame frame;
    public static Dimension w; //window
    private static javax.swing.Timer timer;
    private Font f;
    private Graphics2D g2;
    private int lineCount;
    private MovingLine[] line;
    private long startTime;
    public static final float MAX_SPEED = 8;
    public static final float MIN_SPEED = 2;
    public static final int MAX_LENGTH = 200;
    public static final int MIN_LENGTH = 50;
    public static final boolean AA_ON = true;
    private static final int REFRESH_RATE = 30; //ms

    public Main() {
        this.setFocusable(true);

        System.out.println("MAX_SPEED " + MAX_SPEED);
        System.out.println("MIN_SPEED " + MIN_SPEED);
        System.out.println("MAX_HEIGHT " + MAX_LENGTH);
        System.out.println("MIN_HEIGHT " + MIN_LENGTH);
        System.out.println("REFRESH_RATE " + REFRESH_RATE);
        String AA = AA_ON ? "TRUE" : "FALSE";
        System.out.println("AA_ON " + AA);
        /* there shouldn't be any resizing
        this.addComponentListener(new ComponentListener() {
        public void componentResized(ComponentEvent e){ w = e.getComponent().getSize(); }
        public void componentMoved(ComponentEvent e)  { w = e.getComponent().getSize(); }
        public void componentShown(ComponentEvent e)  { w = e.getComponent().getSize(); }
        public void componentHidden(ComponentEvent e) { w = e.getComponent().getSize(); }
        });
        //*/

        startTime = System.currentTimeMillis();

        this.addMouseMotionListener(new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
                if (System.currentTimeMillis() - startTime > SAFE_TIME) {
                    //System.exit(0);
                }
            }

            public void mouseMoved(MouseEvent e) {
                if (System.currentTimeMillis() - startTime > SAFE_TIME) {
                    //System.exit(0);
                }
            }
        });
        this.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }

            public void mousePressed(MouseEvent e) {
                System.exit(0);
            }

            public void mouseReleased(MouseEvent e) {
                System.exit(0);
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
                //System.exit(0);
            }
        });
        //*
        this.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {
                //System.exit(0);
            }

            public void keyPressed(KeyEvent e) {
                //System.exit(0);
            }

            public void keyReleased(KeyEvent e) {
                //System.exit(0);
            }
        });//*/

        w = Toolkit.getDefaultToolkit().getScreenSize();
        InputStream is = Main.class.getResourceAsStream("/font/digital.ttf"); //failed
        try {
            f = Font.createFont(Font.TRUETYPE_FONT, is);
            f = f.deriveFont(Font.PLAIN, 30);
        } catch (FontFormatException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            f = new Font("Monospaced", Font.BOLD, 30);
            System.err.println("The font could not be loaded!");
            //ex.printStackTrace();
        }

        lineCount = (int)(w.height / 25); //determines number of lines from screen
        //System.out.println("Creating "+lineCount+" lines for res "+w.width+"x"+w.height);
        line = new MovingLine[lineCount];
        for (int i=0; i<lineCount; i++) {
            line[i] = (new MovingLine(i*25));
        }

        timer = new Timer(REFRESH_RATE, this);
    } //end constructor

    @Override
    public void paint(Graphics g) {
        g.setFont(f); //must be here
        g2 = (Graphics2D) g;
        g2.clearRect(0, 0, w.width, w.height);
        if (AA_ON) {
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        }

        for (int i=0; i<lineCount; i++) {
            line[i].update(g2);
        }
    } //end paint

    public void actionPerformed(ActionEvent e) { //called by timer
        if (System.currentTimeMillis() - startTime > SAFE_TIME && !cursorHidden) {
            BufferedImage bi = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
            Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(bi, new Point(), "asdf");
            this.setCursor(cur);
            cursorHidden = true;
        }
        repaint();
    }

    public static void createAndShowGUI() {
        //Create and set up the window.
        frame = new JFrame("Example ScreenSaver");
        frame.setBackground(Color.black);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(0, 0);
        frame.setMinimumSize(Toolkit.getDefaultToolkit().getScreenSize()); // maximize
        frame.setUndecorated(true); //turn off top bar
        Main pnl = new Main();
        pnl.setBackground(Color.black); //just incase
        frame.add(pnl);
        //Display the window.
        frame.pack();
        frame.setVisible(true);
        pnl.requestFocusInWindow();
        w = frame.getSize();

    }

    public static void main(String args[]) {
        System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Example ScreenSaver"); //this doesnt work!
        
        //not sure if these do anything
        System.setProperty("apple.awt.antialiasing", "true");
        System.setProperty("apple.awt.textantialiasing", "true");

        //these appear to make the program run even slower
        //System.setProperty("sun.java2d.opengl", "true");
        //System.setProperty("apple.awt.graphics.UseQuartz","true");
        //System.setProperty("apple.awt.graphics.EnableQ2DX","true");

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
                timer.start();
            }
        });

    } // end main
} //end class

